package kjh820909.swing;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

import kjh820909.dao.ZipcodeDAO;

public class ZipcodeSub extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField dong;
	private JTextField etcAddrA;
	private JComboBox baseAddr = new JComboBox();
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			ZipcodeSub dialog = new ZipcodeSub(null,null,null,null);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
//	public ZipcodeSub() {
	public ZipcodeSub(final JTextField zip1, final JTextField zip2,final JTextField addr, final JTextField etcAddr) {
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		JLabel label = new JLabel("동이름");
		label.setFont(new Font("맑은 고딕", Font.BOLD, 14));
		dong = new JTextField();
		dong.setColumns(10);
		JButton findBtn = new JButton("주소검색");
		findBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				// 동을 입력하지 않고 검색버튼을 누른경우
				// 경고창 표시
				if (dong.getText().equals("")){
					JOptionPane.showMessageDialog(null,	"동을 입력하세요!");
					return;
				}
				
				
				/*String[] addr = new String[]{
						"123-456 서울시 마포구 동교동 502", 
						"134-234 인천시 부평구 십정동 102", 
						"142-321 서울시 영등포구 여의도동 403번지" 
				};*/ // 테스트 코드
				
				// zipCode 테이블에서 동으로 검색한 결과를
				// ArrayList 객체에 담아주면
				// JComboBox 객체에 목록으로 추가할 수 있도록
				// String[] 배열로 다시 작성
				ArrayList<String> zip = 
						ZipcodeDAO.findzip(dong.getText());
				
				// ArrayList에 담긴 내용을 배열로 옮겨 담음
				String[] addr = new String[zip.size()];
				for (int i = 0; i < zip.size(); ++i) {
					addr[i] = zip.get(i);
				}
				
				//baseAddr = new JComboBox(addr); 
				//baseAddr.repaint(); // 불가
				// 콤보박스에 목록을 변경하려면
				// 기존 목록을 모두 지우고 난후
				// addItem 메서드로 새로 추가해야 함
				baseAddr.removeAllItems(); // 기존 목록 제거
				for(String l : addr)
					baseAddr.addItem(l); // 목록 새로 추가
			}
		});
		findBtn.setFont(new Font("맑은 고딕", Font.BOLD, 12));
		
		
		etcAddrA = new JTextField();
		etcAddr.setColumns(10);
		
		JButton sendBtn = new JButton("선택후 닫기");
		sendBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 주소검색 후 적절한 우편번호 및 주소를 선택하고 
				// 나머지 주소를 입력한 후 ZipcodeMain의
				// zip1,zip2,addr,etcAddr에 
				// 자동으로 입력하고 현재창 닫음
				String tmp1 = (String) baseAddr.getSelectedItem();
				//String zipA = tmp1.split(" ")[0].split("-")[0];
				//String zipB = tmp1.split(" ")[0].split("-")[1];
				// 123456 : substring(0, 3) : 0~2 -> 123
				// 123456 : substring(3, 6) : 3~5 -> 456
				String zipA = tmp1.split(" ")[0].substring(0,3);
				String zipB = tmp1.split(" ")[0].substring(3,6);
				String addrA = tmp1.substring(7);
				
				// ZipcodeMain의 zip1,zip2,addr,etcAddr에 
				// 사용자가 선택하고 입력한 값 전달
				zip1.setText(zipA);
				zip2.setText(zipB);
				addr.setText(addrA);
				etcAddr.setText(etcAddrA.getText());
				// 창닫기
				dispose();
			}
		});
		
		JButton notBtn = new JButton("검색 취소");
		notBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		notBtn.setPreferredSize(new Dimension(97, 23));
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_contentPanel.createSequentialGroup()
					.addContainerGap()
					.addComponent(label)
					.addGap(18)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.TRAILING)
						.addComponent(etcAddr, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
						.addComponent(baseAddr, Alignment.LEADING, 0, 281, Short.MAX_VALUE)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(dong, GroupLayout.PREFERRED_SIZE, 139, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED, 61, Short.MAX_VALUE)
							.addComponent(findBtn))
						.addGroup(Alignment.LEADING, gl_contentPanel.createSequentialGroup()
							.addComponent(sendBtn)
							.addPreferredGap(ComponentPlacement.RELATED, 99, Short.MAX_VALUE)
							.addComponent(notBtn, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addGap(71))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(28)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(label)
						.addComponent(findBtn)
						.addComponent(dong, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(28)
					.addComponent(baseAddr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(etcAddr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(27)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(sendBtn)
						.addComponent(notBtn, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(27, Short.MAX_VALUE))
		);
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						dispose();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
}

